package it.desertsupport.hibernate.dao;

import it.desertsupport.model.Defence;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

public class DefenceDAOImpl extends BaseHibernateDAO implements DefenceDAO {

	@Override
	public void save(Defence defence) throws Exception {
		getSession().save(defence);
	}

	@Override
	public void delete(Defence defence) throws Exception {
		getSession().delete(defence);
	}

	@Override
	public Defence getDefence(Long id) throws Exception {
		return (Defence) getSession().get(Defence.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Defence> findAll() throws Exception {
		String queryString = "from Defence";
		Query queryObject = getSession().createQuery(queryString);
		return queryObject.list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Defence> findByExample(Defence defence) throws Exception {
		Criteria cri = getSession().createCriteria(Defence.class);
		cri.add(Example.create(defence));
		return cri.list();
	}

	@Override
	public Defence marge(Defence defence) throws Exception {
		return (Defence) getSession().merge(defence);
	}

}
